/*
 * @Author: ChunLai
 * @Date: 2021-11-15 17:25:00
 * @LastEditTime: 2025-08-21 15:55:12
 * @Description: 用户告知公共方法
 * @FilePath: \02.bldinsure\src\translate\healthy\common.js
 */
import consts from "@/config/const.js";
/* ======= 投被保人告知 ======= */
// 1. 填写规则校验
export const validateHealthRule = (healthyDatas) => {
  // console.log('validate_healthyDatas', healthyDatas);
  let obj = {
    msg: "",
    idx: "",
    status: true,
  };
  const dataArr = healthyDatas.questionsList,
    len = dataArr.length;
  for (let i = 0; i < len; i++) {
    const item = dataArr[i],
      errIdx = `${healthyDatas.type}-${i}`;
    if (item.impartCheck === "1" || item.impartYesOrNo === "Y") {
      // 无按钮必答题目 || 有按钮 && 选是
      const itemArr = item.answer;
      for (let j = 0; j < itemArr.length; j++) {
        const ele = itemArr[j];
        if (ele.check && ele.check === "1" && ele.value === "") {
          const mdName = `${healthyDatas.moduleName}`,
            subNo = `${item.subjectNum || i + 1}`;
          return setError(`请完成${mdName}：第${subNo}题填写`, `${errIdx}`);
        }
      }
    } else if (item.rightBtns) {
      if (item.impartYesOrNo === "") {
        // 有按钮,没有选择的
        return setError(
          `请回答${healthyDatas.moduleName}：第${i + 1}题`,
          `${errIdx}`
        );
      } else if (
        item.fixBtnValue !== void 0 &&
        item.fixBtnValue != item.impartYesOrNo
      ) {
        // 有按钮,没有选择的
        return setError(`${item.errorText}`, `${errIdx}`);
      }
    }
  }
  return obj;
};
// 2.用户填写数据--->为接口数据
export const pageToApi = (healthyDatas) => {
  // console.log('pageToApi---1', healthyDatas);
  let resArr = [];
  for (let i = 0; i < healthyDatas.length; i++) {
    const item = healthyDatas[i].questionsList;
    for (let j = 0; j < item.length; j++) {
      const ele = item[j];
      /*******************************统一格式*************************************************** */
      let obj = {
        impartCode: ele.impartCode,
        impartContent: ele.impartContent,
        impartRemark: "",
        impartYesOrNo: ele.impartYesOrNo,
        impartVersion: ele.impartVersion,
      };
      let impartRemark = [];
      // 选否,传空格逗号
      if (ele.answer && ele.answer.length > 0) {
        if (ele.multans) {
          ele.answer.forEach((btns, isd) => {
            // 选项为是 或者没有是否的 录入答案
            let ansstr = btns.Map((cell) => cell.value);
            impartRemark.push(ansstr.join(","));
          });
        } else {
          let strs = ele.answer.map((cell) => cell.value).join(",");
          impartRemark.push(strs);
        }
      }
      obj.impartRemark = impartRemark.join("#");
      /************************************************************************************** */
      resArr.push(JSON.parse(JSON.stringify(obj)));
    }
    // console.log('pageToApi', resArr);
  }
  return resArr;
};
// 3.接口数据转化回显
export const apiToPageData = (pageData, echos) => {
  if (consts.array(echos)) {
    // console.log('apiToPageData', echos);
    for (let i = 0; i < pageData.length; i++) {
      const item = pageData[i].questionsList;
      for (let j = 0; j < item.length; j++) {
        const itemJ = item[j],
          itemK = itemJ.answer;
        /*******************************统一格式*************************************************** */
        let targetItem = echos.find(
          (obj) =>
            obj.impartCode === itemJ.impartCode &&
            obj.impartVersion === itemJ.impartVersion
        );
        if (targetItem) {
          if (targetItem.impartYesOrNo.indexOf("N") !== -1) {
            itemJ.impartYesOrNo = "N";
          } else {
            itemJ.impartYesOrNo = targetItem.impartYesOrNo;
          }
          if (targetItem.impartRemark !== "") {
            const daanArr = targetItem.impartRemark.split(",");
            itemK.forEach((quebtn, quei) => {
              quebtn.value = daanArr[`${quei}`];
            });
          }
          // 待定
          /*合众 subjectLimit 题目列表: 被关联的题目,有值则显示*/
        }
        /**************************************************************************************** */
      }
    }
  }
  return pageData;
};

// 工具函数：设置错误信息并返回结果对象
const setError = (msg, idx) => {
  res.msg = msg;
  res.idx = idx;
  res.status = false;
  return res;
};
